//
//  ReleaseStep4ViewController.m
//  Gunz
//
//  Created by Joggy on 16/4/16.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "ReleaseStep4ViewController.h"
#import "GZConst.h"
#import "BarButtonView.h"
#import "Step4Select.h"
#import "ReleaseStep5ViewController.h"
#import "QYNotice.h"

@interface ReleaseStep4ViewController ()<UIScrollViewDelegate, UITextFieldDelegate>

@end

@implementation ReleaseStep4ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    styleArr = [[NSMutableArray alloc] initWithCapacity:0];
    styleTitle = @[@"文本", @"图片", @"视频", @"音频", @"调查"];
    [self prepareForNav];
    [self prepareForLayout];
    textBottom = 0;
    scrollOrigin = scroll.frame;
    [scroll addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)]];
    NSLog(@"%@", _data);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"任务发布-步骤4";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [GZConst getScreenWidth], [GZConst getScreenHeight] - 64 - 56)];
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    scroll.delegate = self;
    [self.view addSubview:scroll];
    //底部按钮
    BarButtonView *barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - BBViewHeight, [GZConst getScreenWidth], BBViewHeight) name:@"下一步"];
    [barButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
    //标题lab
    UILabel *titleConfig = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 56, 17)];
    titleConfig.layer.position = CGPointMake([GZConst getScreenWidth]/2, 34);
    titleConfig.textColor = CUSTOM_GREEN;
    titleConfig.font = [UIFont systemFontOfSize:14];
    titleConfig.textAlignment = NSTextAlignmentCenter;
    titleConfig.text = @"数据格式";
    [scroll addSubview:titleConfig];
    //
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 110*proportion, 17)];
    titleLab.textColor = TEXT_TITLE_COLOR;
    titleLab.font = [UIFont systemFontOfSize:14];
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.text = @"数据提交类型:";
    [scroll addSubview:titleLab];
    for(int i = 0; i < styleTitle.count; i++) {
        if(i <= 0) {
            Step4Select *text = [[Step4Select alloc] initWithTitle:styleTitle[i]];
            text.layer.position = CGPointMake(titleLab.frame.origin.x + titleLab.frame.size.width + 27 + 23, titleLab.layer.position.y);
            [text.pointButton addTarget:self action:@selector(pointSelect:) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:text];
            [styleArr addObject:text];
        }
        else {
            Step4Select *tem = styleArr[i - 1];
            Step4Select *text1 = [[Step4Select alloc] initWithTitle:styleTitle[i]];
            text1.layer.position = CGPointMake(tem.layer.position.x, tem.layer.position.y + 32);
            [text1.pointButton addTarget:self action:@selector(pointSelect:) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:text1];
            [styleArr addObject:text1];
        }
    }
    dataFormat = [[Step3InputField alloc] initWithTitle:@"数据提交格式:"];
    Step4Select *te = [styleArr lastObject];
    dataFormat.layer.position = CGPointMake(screenWidth/2, te.layer.position.y + 36);
    dataFormat.textField.delegate = self;
    [scroll addSubview:dataFormat];
    [self adjustScrollContent:dataFormat.frame.origin.y + dataFormat.frame.size.height];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma 事件方法

- (void)pointSelect:(UIButton*)button {
    for(int i = 0; i < styleArr.count; i++) {
        Step4Select *temp = styleArr[i];
        if(temp.pointButton != button) {
            temp.pointButton.selected = false;
        }
    }
    button.selected = !button.selected;
}

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (NSString*)codeForDataFormat:(NSString*)str {
    if([str isEqualToString:@"文本"]) {
        return @"1";
    }
    if([str isEqualToString:@"图片"]) {
        return @"2";
    }
    if([str isEqualToString:@"音频"]) {
        return @"3";
    }
    if([str isEqualToString:@"视频"]) {
        return @"4";
    }
    if([str isEqualToString:@"调查"]) {
        return @"5";
    }
    return @"0";
}

- (NSString*)collectData {
    for(int i = 0; i<styleArr.count; i++) {
        Step4Select *selec = styleArr[i];
        if(selec.pointButton.selected) {
            _data[@"datatype"] = [self codeForDataFormat:selec.titleLabel.text];
            break;
        }
        if(i >= styleArr.count - 1) {
            return @"请选择数据提交类型";
        }
    }
    if([dataFormat.textField.text isEqualToString:@""]) {
        return @"请输入数据提交格式";
    }
    _data[@"dataformat"] = @"0";
    return @"noerror";
}

- (void)nextStep {
    NSString *errorInfo = [self collectData];
    if([errorInfo isEqualToString:@"noerror"]) {
        ReleaseStep5ViewController *next = [[ReleaseStep5ViewController alloc] init];
        next.data = _data;
        [self.navigationController pushViewController:next animated:true];
    }
    else {
        [QYNotice showError:errorInfo toView:self.view];
        [QYNotice dismissWithDelay:3];
    }
}

- (void)adjustScrollContent:(CGFloat)bottom {
    if(scroll.frame.size.height < bottom) {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, bottom);
    }
    else {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    }
}

#pragma 代理方法

- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *dic = notification.userInfo;
    CGFloat keyboardHeight = [[dic objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGFloat adjust = textBottom - ([GZConst getScreenHeight] - keyboardHeight - 64);
    CGRect temp = scrollOrigin;
    if(adjust > 0) {
        temp.origin.y -= adjust;
        [UIView animateWithDuration:0.3 animations:^{
            scroll.frame = temp;
        }];
    }
    else {
        [UIView animateWithDuration:0.3 animations:^{
            scroll.frame = temp;
        }];
    }
}

- (void)keyboardWillHide:(NSNotification*)notification {
    [UIView animateWithDuration:0.3 animations:^{
        scroll.frame = scrollOrigin;
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textBottom = textField.frame.origin.y + textField.frame.size.height + textField.superview.frame.origin.y;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self endEdit];
}

@end
